import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;

import java.io.IOException;
import java.security.MessageDigest;
import java.util.Date;

/**
 * Created by xiang.yu on 2017/7/10.
 */
public class CheckSumBuilder {
    // 计算并获取CheckSum
    public static String getCheckSum(String appSecret, String nonce, String curTime) {
        return encode("sha1", appSecret + nonce + curTime);
    }

    // 计算并获取md5值
    public static String getMD5(String requestBody) {
        return encode("md5", requestBody);
    }

    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }

    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};


    public static void main(String[] args) {
        String appKey = "162d05aab5ec45e76833174a06b6429e";
        String appSecrt = "3feba7dc5679";
        String num = "2";
        String time = String.valueOf(new Date().getTime() / 1000);
        String str = CheckSumBuilder.getCheckSum(appSecrt, num, time);
        System.out.println(str);

        PostMethod postMethod = new PostMethod("https://api.netease.im/nimserver/chatroom/create.action");
        // 填入各个表单域的值
        NameValuePair[] data = {
                new NameValuePair("creator", "helloworld"),
                new NameValuePair("name", "this.is test"),
                new NameValuePair("announcement", ""),
                new NameValuePair("broadcasturl", ""),
                new NameValuePair("ext", "")};
        // 将表单的值放入postMethod中
        postMethod.addRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        postMethod.setRequestBody(data);
        postMethod.setRequestHeader("AppKey", appKey);
        postMethod.setRequestHeader("Nonce", num);
        postMethod.setRequestHeader("CurTime", time);
        postMethod.setRequestHeader("CheckSum", str);
        // 执行postMethod
        int statusCode = 0;
        try {
            org.apache.commons.httpclient.HttpClient httpClient = new org.apache.commons.httpclient.HttpClient();
            statusCode = httpClient.executeMethod(postMethod);
            String result = postMethod.getResponseBodyAsString();
            System.out.println(statusCode);
            System.out.println(result);
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
